﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TunnelsOfDeath.Managers
{
    class MapManager
    {

        public bool IsPositionListUpdated { get; set; }

        public MapManager()
        {
            IsPositionListUpdated = true;
        }

        public void ReloadCurrentMap()
        {
            LoadMap(Globals.CurrentMapName);
        }

        public void LoadFirstMap()
        {
            LoadSave.MapLoader loader = new LoadSave.MapLoader(Globals.CurrentMapPack);
            loader.LoadFirstMap();
            RefreshPositionUpdate();
        }

        public bool LoadNextMap()
        {
            LoadSave.MapLoader loader = new LoadSave.MapLoader(Globals.CurrentMapPack);
            string nextMap = loader.GetNameOfNextMap(Globals.CurrentMapName);
            if (string.IsNullOrWhiteSpace(nextMap))
            {
                return false;
            }
            else
            {
                loader.LoadMap(loader.GetNameOfNextMap(Globals.CurrentMapName));
                RefreshPositionUpdate();
                return true;
            }

        }

        public void LoadMap(string _name)
        {
            LoadSave.MapLoader loader = new LoadSave.MapLoader(Globals.CurrentMapPack);
            loader.LoadMap(_name);
            RefreshPositionUpdate();
        }

        public void GetRandomMap(int _x, int _y)
        {
            Globals.Map = new Objects.Sprite[_x, _y];
            Objects.Sprite toRegister = null;

            for (int x = 0; x < _x; x++)
            {
                for (int y = 0; y < _y; y++)
                {

                    if (x==0 || y == 0 || x == _x-1 || y == _y -1)
                    {
                        Globals.ObjectManager.RegisterObject(Globals.MainGenerator.GetWall(new Microsoft.Xna.Framework.Vector2(x, y)));
                        continue;
                    }

                    int rand = Globals.random.Next(1, 15);

                    switch (rand)
                    {
                        case 1:
                            toRegister = Globals.MainGenerator.GetDirt(new Microsoft.Xna.Framework.Vector2(x, y));
                            break;
                        case 2:
                            toRegister = Globals.MainGenerator.GetRock(new Microsoft.Xna.Framework.Vector2(x, y));
                            break;
                        case 3:
                            toRegister = null;
                            break;
                        case 4:
                            toRegister = Globals.MainGenerator.GetGold(new Microsoft.Xna.Framework.Vector2(x, y));
                            break;
                        default:
                            toRegister = Globals.MainGenerator.GetDirt(new Microsoft.Xna.Framework.Vector2(x, y));
                            break;
                    }

                    Globals.ObjectManager.RegisterObject(toRegister);
                    
                }
            }

            toRegister = Globals.MainGenerator.GetPlayer(new Microsoft.Xna.Framework.Vector2(2, 1),Objects.ControllerSource.KEYBOARD,1);
            Globals.ObjectManager.RegisterObject(toRegister);

            //toRegister = Globals.MainGenerator.GetPlayer(new Microsoft.Xna.Framework.Vector2(4, 1), Objects.ControllerSource.GAMEPAD1,2);
            //Globals.ObjectManager.RegisterObject(toRegister);

            toRegister = Globals.MainGenerator.GetEnemy(new Microsoft.Xna.Framework.Vector2(6, 6),false,1);
            Globals.ObjectManager.RegisterObject(toRegister);

            toRegister = Globals.MainGenerator.GetDoor(new Microsoft.Xna.Framework.Vector2(0, 5),2);
            Globals.ObjectManager.RegisterObject(toRegister);

            RefreshPositionUpdate();
            
        }
 
        public void RefreshPositionUpdate()
        {
            Globals.PositionUpdate.Clear();

            for (int x = 0; x < Globals.Map.GetLength(0); x++)
            {
                for (int y = 0; y < Globals.Map.GetLength(1); y++)
                {

                    Objects.Sprite myObject = Globals.Map[x,y];

                    if (myObject != null && (myObject.Type != ObjectType.dirt || myObject.Type != ObjectType.wall))
                    {
                        Globals.PositionUpdate.Add(myObject);
                    }


                }
            }

            SortList();

        }

        public void SortList()
        {
            if (!IsPositionListUpdated)
            {
                Globals.PositionUpdate.Sort();
                IsPositionListUpdated = true;
            }
        }

    }
}
